@extends('admin.main')

@section('content')
<div class="wrapper wrapper-content">
    <div class="m-b">
        <a href="javascript:;" class="btn btn-default" onclick="history.back();"><i class="fa fa-angle-double-left"></i> 返回列表</a>
    </div>
    <div class="ibox float-e-margins">
        <div class="ibox-title">
            <h5>阅读理解 <i class="fa fa-angle-double-right m-l m-r"></i> 编辑</h5>
        </div>
        <div class="ibox-content">
            <form id="formEdit" method="post" class="form-horizontal" onsubmit="return false;">
                <div class="form-group">
                    <label class="col-lg-2 control-label">阅读：</label>
                    <div class="col-lg-10">
                        <div id="ueditor_topic" class="summernote">{!!$topic!!}</div>
                    </div>
                </div>
                <div id="readingOptions">
                    @foreach($rows as $i => $row)
                        <div class="form-group" style="border-top:1px dashed #e7eaec;margin-bottom:0;">
                            <label class="col-lg-2 control-label">题目：</label>
                            <div class="col-lg-10">
                                <div class="ibox">
                                    <div class="ibox-tools">
                                        @if($i < 1)
                                        <a class="js-copy text-primary"> <i class="fa fa-copy"></i> 复制</a>
                                        @else
                                        <a class="js-remove text-danger"><i class="fa fa-times"></i> 删除</a>
                                        @endif
                                    </div>
                                    <div class="col-lg-12 m-b chat">
                                        <textarea name="subtopic[]" rows="2" class="form-control">{{$row->topic}}</textarea>
                                        <input type="hidden" name="answer[]" value="{{$row->answer}}" />
                                    </div>
                                    @foreach($row->options as $key => $val)
                                    <div class="col-lg-6">
                                        <div class="input-group">
                                            <span class="input-group-btn">
                                                <button type="button" class="btn btn-{{$row->answer == $key ? 'primary' : 'white'}}">{{$key}}</button>
                                            </span>
                                            <input type="text" name="options[]" value="{{$val}}" placeholder="" class="form-control" />
                                        </div>
                                    </div>
                                    @endforeach
                                </div>
                            </div>
                        </div>
                    @endforeach
                </div>
                <div class="hr-line-dashed"></div>
                <div class="form-group">
                    <div class="col-lg-4 col-lg-offset-2">
                        <input type="hidden" name="id" value="{{$id}}" />
                        <button class="btn btn-warning btn-w-m" type="submit">提交</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@section('pageheader')
<link href="/assets/iCheck/custom.css" rel="stylesheet">
<link href="/assets/summernote/summernote.css" rel="stylesheet">
<link href="/assets/summernote/summernote-bs3.css" rel="stylesheet">
@endsection

@section('pagescript')
<script src="/assets/iCheck/icheck.min.js"></script>
<script src="/assets/summernote/summernote.min.js"></script>
<script>
    $("#side-menu li[rel='examtopic']").addClass("active")
        .parents("ul").addClass("in").parents("li").addClass("active");

    // Summernote 编辑器上传图片
    var sendFile = function (file, editor, welEditable) {
        var data = new FormData();
        data.append("upfile", file, file.name || ('blob.' + file.type.substr('image/'.length)));
        $.ajax({
            data: data,
            type: "POST",
            dataType: "json",
            url: "{{url('ueditor?action=uploadimage')}}",
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                editor.insertImage(welEditable, data.url);
            }
        });
    };

    // 加载 Summernote 编辑器
    var loadSummernote = function(editor){
        editor.summernote({
            onImageUpload: function(files, editor, welEditable) {
                sendFile(files[0], editor, welEditable);
            }
        });
    };

    // 增加阅读理解题目
    $("#readingOptions").delegate(".js-copy", "click", function(){
        var html = $(this).parents(".form-group").clone();
        $(html).find(".js-copy").after('<a class="js-remove text-danger"><i class="fa fa-times"></i> \u5220\u9664</a>').remove();
        $("#readingOptions").append(html);
    });

    // 设置阅读理解题目答案
    $("#readingOptions").delegate(".btn", "click", function(){
        $(this).parents(".ibox").eq(0).find(".btn").each(function(){
            $(this).removeClass("btn-primary").addClass("btn-white");
        });
        $(this).removeClass("btn-white").addClass("btn-primary");
        $(this).parents(".form-group").eq(0).find("input[name='answer[]']").val($(this).text());
    });

    // 删除阅读理解题目
    $("#readingOptions").delegate(".js-remove", "click", function(){
        $(this).parents(".form-group").remove();
    });

    $("#formEdit").on("submit", function(){
        var form = this, formdata = new FormData();
        formdata.append("topic", $("#ueditor_topic").code());
        $.each($(form).serializeArray(), function(i, item) {
            formdata.append(item.name, item.value);
        });
        $.ajax({
            url: "{{url('admin/exam/update')}}",
            type: "post",
            processData: false,
            contentType: false,
            dataType: "json",
            data: formdata,
            success:function(){
                window.location.href = document.referrer;
            },
            error:failure
        });
        return false;
    });

    $(function(){
        loadSummernote($('#ueditor_topic'));
    });
</script>
@endsection
